package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.PigTypeDAO;
import cn.edu.scau.cmi.hombio.dao.SaleTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.PigType;
import cn.edu.scau.cmi.hombio.domain.SaleTaskQuality;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for PigType entities
 * 
 */

@Service("PigTypeService")

@Transactional
public class PigTypeServiceImpl implements PigTypeService {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages PigType entities
	 * 
	 */
	@Autowired
	private PigTypeDAO pigTypeDAO;

	/**
	 * DAO injected by Spring that manages SaleTaskQuality entities
	 * 
	 */
	@Autowired
	private SaleTaskQualityDAO saleTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * Instantiates a new PigTypeServiceImpl.
	 *
	 */
	public PigTypeServiceImpl() {
	}

	/**
	 * Return all PigType entity
	 * 
	 */
	@Transactional
	public List<PigType> findAllPigTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<PigType>(pigTypeDAO.findAllPigTypes(startResult, maxRows));
	}

	/**
	 * Save an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public PigType savePigTypeSaleTaskQualities(Integer id, SaleTaskQuality related_saletaskqualities) {
		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(id, -1, -1);
		SaleTaskQuality existingsaleTaskQualities = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(related_saletaskqualities.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsaleTaskQualities != null) {
			existingsaleTaskQualities.setId(related_saletaskqualities.getId());
			existingsaleTaskQualities.setPrice(related_saletaskqualities.getPrice());
			existingsaleTaskQualities.setQuality(related_saletaskqualities.getQuality());
			existingsaleTaskQualities.setMemo(related_saletaskqualities.getMemo());
			related_saletaskqualities = existingsaleTaskQualities;
		}

		related_saletaskqualities.setPigType(pigtype);
		pigtype.getSaleTaskQualities().add(related_saletaskqualities);
		related_saletaskqualities = saleTaskQualityDAO.store(related_saletaskqualities);
		saleTaskQualityDAO.flush();

		pigtype = pigTypeDAO.store(pigtype);
		pigTypeDAO.flush();

		return pigtype;
	}

	/**
	 * Delete an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public PigType deletePigTypeTaskPigsties(Integer pigtype_id, Integer related_taskpigsties_id) {
		TaskPigsty related_taskpigsties = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties_id, -1, -1);

		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id, -1, -1);

		related_taskpigsties.setPigType(null);
		pigtype.getTaskPigsties().remove(related_taskpigsties);

		taskPigstyDAO.remove(related_taskpigsties);
		taskPigstyDAO.flush();

		return pigtype;
	}

	/**
	 * Save an existing PigType entity
	 * 
	 */
	@Transactional
	public void savePigType(PigType pigtype) {
		PigType existingPigType = pigTypeDAO.findPigTypeByPrimaryKey(pigtype.getId());

		if (existingPigType != null) {
			if (existingPigType != pigtype) {
				existingPigType.setId(pigtype.getId());
				existingPigType.setName(pigtype.getName());
				existingPigType.setMemo(pigtype.getMemo());
			}
			pigtype = pigTypeDAO.store(existingPigType);
		} else {
			pigtype = pigTypeDAO.store(pigtype);
		}
		pigTypeDAO.flush();
	}

	/**
	 * Return a count of all PigType entity
	 * 
	 */
	@Transactional
	public Integer countPigTypes() {
		return ((Long) pigTypeDAO.createQuerySingleResult("select count(o) from PigType o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public PigType deletePigTypePigs(Integer pigtype_id, Integer related_pigs_id) {
		Pig related_pigs = pigDAO.findPigByPrimaryKey(related_pigs_id, -1, -1);

		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id, -1, -1);

		related_pigs.setPigType(null);
		pigtype.getPigs().remove(related_pigs);

		pigDAO.remove(related_pigs);
		pigDAO.flush();

		return pigtype;
	}

	/**
	 * Save an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public PigType savePigTypeTaskPigsties(Integer id, TaskPigsty related_taskpigsties) {
		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(id, -1, -1);
		TaskPigsty existingtaskPigsties = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPigsties != null) {
			existingtaskPigsties.setId(related_taskpigsties.getId());
			existingtaskPigsties.setMemo(related_taskpigsties.getMemo());
			related_taskpigsties = existingtaskPigsties;
		} else {
			related_taskpigsties = taskPigstyDAO.store(related_taskpigsties);
			taskPigstyDAO.flush();
		}

		related_taskpigsties.setPigType(pigtype);
		pigtype.getTaskPigsties().add(related_taskpigsties);
		related_taskpigsties = taskPigstyDAO.store(related_taskpigsties);
		taskPigstyDAO.flush();

		pigtype = pigTypeDAO.store(pigtype);
		pigTypeDAO.flush();

		return pigtype;
	}

	/**
	 * Delete an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public PigType deletePigTypeSaleTaskQualities(Integer pigtype_id, Integer related_saletaskqualities_id) {
		SaleTaskQuality related_saletaskqualities = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(related_saletaskqualities_id, -1, -1);

		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id, -1, -1);

		related_saletaskqualities.setPigType(null);
		pigtype.getSaleTaskQualities().remove(related_saletaskqualities);

		saleTaskQualityDAO.remove(related_saletaskqualities);
		saleTaskQualityDAO.flush();

		return pigtype;
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public PigType savePigTypePigs(Integer id, Pig related_pigs) {
		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(id, -1, -1);
		Pig existingpigs = pigDAO.findPigByPrimaryKey(related_pigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigs != null) {
			existingpigs.setId(related_pigs.getId());
			existingpigs.setNumber(related_pigs.getNumber());
			existingpigs.setEarNumber(related_pigs.getEarNumber());
			existingpigs.setRfid(related_pigs.getRfid());
			existingpigs.setBirthday(related_pigs.getBirthday());
			existingpigs.setState(related_pigs.getState());
			existingpigs.setMemo(related_pigs.getMemo());
			related_pigs = existingpigs;
		}

		related_pigs.setPigType(pigtype);
		pigtype.getPigs().add(related_pigs);
		related_pigs = pigDAO.store(related_pigs);
		pigDAO.flush();

		pigtype = pigTypeDAO.store(pigtype);
		pigTypeDAO.flush();

		return pigtype;
	}

	/**
	 * Load an existing PigType entity
	 * 
	 */
	@Transactional
	public Set<PigType> loadPigTypes() {
		return pigTypeDAO.findAllPigTypes();
	}

	/**
	 * Delete an existing PigType entity
	 * 
	 */
	@Transactional
	public void deletePigType(PigType pigtype) {
		pigTypeDAO.remove(pigtype);
		pigTypeDAO.flush();
	}

	/**
	 */
	@Transactional
	public PigType findPigTypeByPrimaryKey(Integer id) {
		return pigTypeDAO.findPigTypeByPrimaryKey(id);
	}
}
